package com.example.stsg.service.impl;

import com.example.stsg.domain.Signup;
import com.example.stsg.domain.StudentInfo;
import com.example.stsg.domain.Users;
import com.example.stsg.mapper.StudentInfoMapper;
import com.example.stsg.mapper.UsersMapper;
import com.example.stsg.service.UserService;
import com.example.stsg.utils.JwtHelper;
import com.example.stsg.utils.MD5Util;
import com.example.stsg.utils.Result;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private StudentInfoMapper studentInfoMapper;

    @Autowired
    private JwtHelper jwtHelper;


    @Override
    public Result signup(Signup signup) {
        Users user = usersMapper.selectByUsername(signup.getUsername());
        String password = MD5Util.encrypt(signup.getPassword());
        if (user == null) {
            Users u = new Users(signup.getUsername(), password);
            usersMapper.insertSelective(u);
            StudentInfo studentInfo = new StudentInfo(u.getUserId(),signup.getClassName(),signup.getTechDirection(),signup.getIsZsb(),signup.getIsPartyMember(),signup.getCompetitionDirection(),signup.getPosition(),signup.getHobbies(),signup.getReligion(),signup.getIsGraduated(),signup.getPhoneNumber() );
            studentInfoMapper.insertSelective(studentInfo);
            return Result.success();
        }
        return Result.error("用户名已被注册");
    }

    @Override
    public Result signin(Users users) {
        Users user = usersMapper.selectByUsername(users.getUsername());
        System.out.println(user);
        if (user != null) {
            if (user.getPassword().equals(MD5Util.encrypt(users.getPassword()))) {
//                String JwtHelper = jwtHelper.createToken(Long.valueOf(user.getUserId()));
                return Result.success();
            }
            return Result.error("密码错误");
        }
        return Result.error("用户名不存在");
    }
}
